/*
 * @Author: 姚杰鸿 adolphus520@163.com
 * @Date: 2024-04-11 16:33:51
 * @LastEditors: 姚杰鸿
 * @LastEditTime: 2024-11-07 14:33:04
 * @Description: file content
 */
import { checkPasswordRule } from "@/lib/passwordCheck";

/**
 * 手机号码校验(第一位为1，后十位为0-9任意数字)
 */
const validatePhone = (rule: any, value: any, callback: any) => {
  // const reg = /^(\+[0-9]{2,}-?)?1[0-9]{10}$/;
  const reg = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/;
  if (!reg.test(value)) {
    callback(new Error("手机号码格式错误"));
  } else {
    callback();
  }
};
/**
 * 邮箱核验
 */
const validateEmail = (rule: any, value: any, callback: any) => {
  // const reg = /^(\+[0-9]{2,}-?)?1[0-9]{10}$/;
  const reg = /^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/;
  if (!reg.test(value)) {
    callback(new Error("邮箱格式错误"));
  } else {
    callback();
  }
};
/**
 *  验证码为6位
 */
const validateCode = (rule: any, value: any, callback: any) => {
  if (value.length != 6) {
    callback(new Error("验证码长度为6位"));
  } else {
    callback();
  }
};
/**
 *  密码校验规则
 */
const validatePassword = (rule: any, value: string, callback: any) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    const name = null;
    const result: string = checkPasswordRule(value, name);
    if (result === "校验通过") {
      callback();
    } else {
      callback(new Error(result));
    }
  }
};

export const phone = [
  {
    required: true,
    message: "手机号不能为空",
    trigger: "blur",
  },
  {
    validator: validatePhone,
    trigger: "blur",
  },
];

export const email = [
  {
    required: true,
    message: "电子邮箱不能为空",
    trigger: "blur",
  },
  {
    validator: validateEmail,
    trigger: "blur",
  },
];

export const password = [
  {
    validator: validatePassword,
    trigger: "blur",
  },
];

export const verificationCode = [
  {
    required: true,
    message: "验证码不能为空",
    trigger: "blur",
  },
  {
    validator: validateCode,
    trigger: "blur",
  },
];
